﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.HVAC.Properties;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYRevitCode.FormUtility;

namespace YArchitech.HVAC.LevelIndependentTag
{
	public partial class FrmLevelIndependentTag : System.Windows.Forms.Form
	{
		public FrmLevelIndependentTag(bool isPanel)
		{
			this.isPlane = isPanel;
			this.LoadPreviewIamge();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.InitializeComponent();
			List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{AE6B1C45-30A4-470A-BEF8-B55490808489}", null);
			base.Icon = YArchitech.LIB.Resources.YArchitech;
			this.userSetting = new UserSetting();
		}

		public PipeOffsetMarkSet PipeOffsetMarkSetting
		{
			get
			{
				return this.pipeOffsetMarkSetting;
			}
			set
			{
				this.pipeOffsetMarkSetting = value;
			}
		}

		public CommonFont CommomFont
		{
			get
			{
				return this.commomFont;
			}
			set
			{
				this.commomFont = value;
			}
		}

		public bool ContorlProcess
		{
			get
			{
				return this.contorlProcess;
			}
			set
			{
				this.contorlProcess = value;
			}
		}

		private void btnAnnSet_Click(object sender, EventArgs e)
		{
			if (this.frmMarkingSetting.ShowDialog() == DialogResult.Cancel)
			{
				return;
			}
			this.userSetting = new UserSetting();
			this.pipeOffsetMarkSetting = this.userSetting.GetOffsetMarkSetting();
			this.commomFont = this.userSetting.GetFontSetting();
			if (this.contorlProcess)
			{
				this.MimicEsc();
			}
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void rdo_CheckedChanged(object sender, EventArgs e)
		{
			this.ContorlPrevieImage();
			this.TagSet();
			if (this.contorlProcess)
			{
				this.MimicEsc();
			}
		}

		private void FrmLevelIndependentTag_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.RecordData();
			this.MimicEsc();
			this.contorlProcess = true;
		}

		private void MimicEsc()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			FrmLevelIndependentTag.SendMessage(PipeIndepententTag.wndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			FrmLevelIndependentTag.keybd_event((byte)bVk, 0, dwFlags, 0);
			FrmLevelIndependentTag.keybd_event((byte)bVk, 0, dwFlags2, 0);
			this.contorlProcess = false;
		}

		private void PlanelContorlPanelDisableOption()
		{
			this.SetContorlEable();
			if (this.rdoSingle.Checked && (!this.rdoNoTriangle.Checked || !this.rdoNoLeadWire.Checked))
			{
				this.rdoOnLine.Checked = false;
				this.rdoBelowLine.Checked = false;
				this.rdoOnLine.Enabled = false;
				this.rdoBelowLine.Enabled = false;
			}
			if (this.rdoMutliple.Checked)
			{
				if (this.pipeOffsetMarkSetting.Content == "绝对标高" || string.IsNullOrEmpty(this.pipeOffsetMarkSetting.Content))
				{
					this.rdoTriangle.Checked = true;
					this.rdoLeadWire.Checked = true;
					this.rdoOnLine.Checked = false;
					this.rdoBelowLine.Checked = false;
					this.rdoNoTriangle.Enabled = false;
					this.rdoNoLeadWire.Enabled = false;
					this.rdoOnLine.Enabled = false;
					this.rdoBelowLine.Enabled = false;
				}
				if (this.pipeOffsetMarkSetting.Content == "相对标高")
				{
					this.rdoNoTriangle.Checked = true;
					this.rdoTriangle.Enabled = false;
					if (this.rdoLeadWire.Checked)
					{
						this.rdoOnLine.Checked = false;
						this.rdoBelowLine.Checked = false;
						this.rdoOnLine.Enabled = false;
						this.rdoBelowLine.Enabled = false;
					}
				}
			}
		}

		private void SetContorlEable()
		{
			this.rdoTriangle.Enabled = true;
			this.rdoNoTriangle.Enabled = true;
			this.rdoSingle.Enabled = true;
			this.rdoMutliple.Enabled = true;
			this.rdoLeadWire.Enabled = true;
			this.rdoNoLeadWire.Enabled = true;
			this.rdoOnLine.Enabled = true;
			this.rdoBelowLine.Enabled = true;
		}

		private void TreeDContorlPanelDisableOption()
		{
			this.rdoTriangle.Enabled = false;
			this.rdoLeadWire.Enabled = false;
			this.rdoMutliple.Enabled = false;
			this.rdoNoTriangle.Checked = true;
			this.rdoNoLeadWire.Checked = true;
			this.rdoSingle.Checked = true;
		}

		public void ContorlDisable()
		{
			if (this.isPlane)
			{
				this.PlanelContorlPanelDisableOption();
			}
			else
			{
				this.TreeDContorlPanelDisableOption();
			}
			this.ContorlPrevieImage();
		}

		public void SetControlsEnable(bool bePlane)
		{
		}

		private void TagSet()
		{
			if (this.rdoTriangle.Checked)
			{
				this.pipeLevTagSet.TagType = true;
			}
			else
			{
				this.pipeLevTagSet.TagType = false;
			}
			if (this.rdoSingle.Checked)
			{
				this.pipeLevTagSet.TagModel = true;
			}
			else
			{
				this.pipeLevTagSet.TagModel = false;
			}
			if (this.rdoLeadWire.Checked)
			{
				this.pipeLevTagSet.TagStyle = true;
			}
			else
			{
				this.pipeLevTagSet.TagStyle = false;
			}
			if (this.rdoBelowLine.Checked)
			{
				this.pipeLevTagSet.TagLocation = false;
				return;
			}
			this.pipeLevTagSet.TagLocation = true;
		}

		private void LoadPreviewIamge()
		{
			this.dicPreviewIamge.Add("SingleAbsulateTrangleLeadWire", YArchitech.HVAC.Properties.Resources.SingleAbsulateTrangleLeadWire);
			this.dicPreviewIamge.Add("SingleAbsulateTrangleNoLeadWire", YArchitech.HVAC.Properties.Resources.SingleAbsulateTrangleNoLeadWire);
			this.dicPreviewIamge.Add("SingleAbsulateNoTrangleLeadWire", YArchitech.HVAC.Properties.Resources.SingleAbsulateNoTrangleLeadWire);
			this.dicPreviewIamge.Add("SingleAbsulateNoTrangleNoLeadWire01", YArchitech.HVAC.Properties.Resources.SingleAbsulateNoTrangleNoLeadWire01);
			this.dicPreviewIamge.Add("SingleAbsulateNoTrangleNoLeadWire02", YArchitech.HVAC.Properties.Resources.SingleAbsulateNoTrangleNoLeadWire02);
			this.dicPreviewIamge.Add("SingleRelativeTrangleLeadWire", YArchitech.HVAC.Properties.Resources.SingleRelativeTrangleLeadWire);
			this.dicPreviewIamge.Add("SingleRelativeTrangleNoLeadWire", YArchitech.HVAC.Properties.Resources.SingleRelativeTrangleNoLeadWire);
			this.dicPreviewIamge.Add("SingleRelativeNoTrangleLeadWire", YArchitech.HVAC.Properties.Resources.SingleRelativeNoTrangleLeadWire);
			this.dicPreviewIamge.Add("SingleRelativeNoTrangleNoLeadWire01", YArchitech.HVAC.Properties.Resources.SingleRelativeNoTrangleNoLeadWire01);
			this.dicPreviewIamge.Add("SingleRelativeNoTrangleNoLeadWire02", YArchitech.HVAC.Properties.Resources.SingleRelativeNoTrangleNoLeadWire02);
			this.dicPreviewIamge.Add("MutileAbsulateTrangleLeadWire", YArchitech.HVAC.Properties.Resources.MutileAbsulateTrangleLeadWire);
			this.dicPreviewIamge.Add("MutileRelativeNoTrangleLeadWire", YArchitech.HVAC.Properties.Resources.MutilRelativeNoTrangleLeadWire);
			this.dicPreviewIamge.Add("MutileRelativeNoTrangleNoLeadWire01", YArchitech.HVAC.Properties.Resources.MutileRelativeNoTrangleNoLeadWire01);
			this.dicPreviewIamge.Add("MutileRelativeNoTrangleNoLeadWire02", YArchitech.HVAC.Properties.Resources.MutileRelativeNoTrangleNoLeadWire02);
		}

		private void ContorlPrevieImage()
		{
			string strKey = "";
			if (this.rdoSingle.Checked)
			{
				strKey = "Single";
			}
			if (this.rdoMutliple.Checked)
			{
				strKey = "Mutile";
			}
			if (this.pipeOffsetMarkSetting.Content == "绝对标高" || string.IsNullOrEmpty(this.pipeOffsetMarkSetting.Content))
			{
				strKey += "Absulate";
			}
			if (this.pipeOffsetMarkSetting.Content == "相对标高")
			{
				strKey += "Relative";
			}
			if (this.rdoTriangle.Checked)
			{
				strKey += "Trangle";
			}
			if (this.rdoNoTriangle.Checked)
			{
				strKey += "NoTrangle";
			}
			if (this.rdoLeadWire.Checked)
			{
				strKey += "LeadWire";
			}
			if (this.rdoNoLeadWire.Checked)
			{
				strKey += "NoLeadWire";
			}
			if (this.rdoOnLine.Enabled && this.rdoOnLine.Enabled)
			{
				if (this.rdoOnLine.Checked)
				{
					strKey += "01";
				}
				if (this.rdoBelowLine.Checked)
				{
					strKey += "02";
				}
			}
			if (this.dicPreviewIamge.FirstOrDefault((KeyValuePair<string, Image> p) => p.Key == strKey).Value != null)
			{
				this.picPreview.Image = this.dicPreviewIamge[strKey];
				return;
			}
			this.picPreview.Image = null;
		}

		private void InitializeData()
		{
			string[] array = this.userSetting.ReadLevIndTagData();
			if (string.IsNullOrEmpty(array[0]) || int.Parse(array[0]) == 0)
			{
				this.rdoTriangle.Checked = true;
			}
			else if (int.Parse(array[0]) == 1)
			{
				this.rdoNoTriangle.Checked = true;
			}
			if (string.IsNullOrEmpty(array[1]) || int.Parse(array[1]) == 0)
			{
				this.rdoSingle.Checked = true;
			}
			else if (int.Parse(array[1]) == 1)
			{
				this.rdoMutliple.Checked = true;
			}
			if (string.IsNullOrEmpty(array[2]) || int.Parse(array[2]) == 0)
			{
				this.rdoNoLeadWire.Checked = true;
			}
			else if (int.Parse(array[2]) == 1)
			{
				this.rdoLeadWire.Checked = true;
			}
			if (string.IsNullOrEmpty(array[3]))
			{
				this.rdoOnLine.Checked = false;
				this.rdoBelowLine.Checked = false;
			}
			else if (int.Parse(array[3]) == 0)
			{
				this.rdoOnLine.Checked = true;
			}
			else if (int.Parse(array[3]) == 1)
			{
				this.rdoBelowLine.Checked = true;
			}
			this.ContorlPrevieImage();
		}

		private void RecordData()
		{
			string[] array = new string[4];
			if (this.rdoTriangle.Checked)
			{
				array[0] = "0";
			}
			else
			{
				array[0] = "1";
			}
			if (this.rdoSingle.Checked)
			{
				array[1] = "0";
			}
			else
			{
				array[1] = "1";
			}
			if (this.rdoNoLeadWire.Checked)
			{
				array[2] = "0";
			}
			else
			{
				array[2] = "1";
			}
			if (this.rdoOnLine.Checked)
			{
				array[3] = "0";
			}
			else if (this.rdoBelowLine.Checked)
			{
				array[3] = "1";
			}
			else
			{
				array[3] = "";
			}
			this.userSetting.WriteLevIndTagData(array);
		}

		private void FrmLevelIndependentTag_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
			this.frmMarkingSetting = new FormTotalMarkSetting(ShowMarkType.Offset);
			this.InitializeData();
			this.pipeOffsetMarkSetting = this.userSetting.GetOffsetMarkSetting();
			this.commomFont = this.userSetting.GetFontSetting();
			base.KeyPreview = true;
		}

		private void FrmLevelIndependentTag_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				this.RecordData();
				this.MimicEsc();
				this.contorlProcess = true;
			}
		}

		private void FrmLevelIndependentTag_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
		}

		private UserSetting userSetting;

		private FormTotalMarkSetting frmMarkingSetting;

		private CommonFont commomFont = new CommonFont();

		private PipeOffsetMarkSet pipeOffsetMarkSetting = new PipeOffsetMarkSet();

		public PipeLevelTagSet pipeLevTagSet = new PipeLevelTagSet();

		private bool contorlProcess;

		public bool isPlane = true;

		private Dictionary<string, Image> dicPreviewIamge = new Dictionary<string, Image>();

		private ControlInfoManager m_thisManger;
	}
}
